package com.zs.leymovie.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.handmark.pulltorefresh.library.ILoadingLayout;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.bitmap.PauseOnScrollListener;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.zs.leymovie.R;
import com.zs.leymovie.flashview.FlashView;
import com.zs.leymovie.flashview.constants.EffectConstants;

import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * 作者：舒纲聿
 * 时间：2015年8月18日14:43:58
 * 功能；封装一些相同的数据,目前没有作用
 */

public abstract class MovieBaseItemFragment extends Fragment implements AdapterView.OnItemClickListener, PullToRefreshBase.OnRefreshListener2 {
    private String url;
    @ViewInject(R.id.movie_item_fg_container)
    private PullToRefreshListView mPtrListview;
    private BitmapUtils bitmapUtils = new BitmapUtils(getActivity());
    private ViewHolder vh;

    public MovieBaseItemFragment() {
    }

    public MovieBaseItemFragment(String url) {
        this.url = url;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_movie_base_item, container, false);
        ViewUtils.inject(this, view);
        initView();
        return view;
    }

    //初始化listview
    private void initView() {
        mPtrListview.setMode(PullToRefreshBase.Mode.PULL_FROM_START);

        //获得刷新的view
        ListView listView = mPtrListview.getRefreshableView();

        //加载头部布局和底部布局
        addView(listView);

        ILoadingLayout loadingLayout = mPtrListview.getLoadingLayoutProxy();

        //listview在快速滑动时默认不加载图片
        mPtrListview.setOnScrollListener(new PauseOnScrollListener(bitmapUtils, true, false));

        //设置上下拉刷新监听
        mPtrListview.setOnRefreshListener(this);
        //设置点击事件
        mPtrListview.setOnItemClickListener(this);
        //上下拉动时展现view
        ILoadingLayout headLayout = mPtrListview.getLoadingLayoutProxy(true, false);
        ILoadingLayout footerLayout = mPtrListview.getLoadingLayoutProxy(false, true);
        mPtrListview.setRefreshing();

    }

    private void addView(ListView listView) {
        LayoutInflater inflater = getActivity().getLayoutInflater();
        View headView = inflater.inflate(R.layout.movie_lv_head_vp, null);
        View footerView = inflater.inflate(R.layout.movie_lv_footer_text, null);
        listView.addHeaderView(headView);
        vh = new ViewHolder(headView);
        listView.addFooterView(footerView);
    }

    @Override
    public abstract void onItemClick(AdapterView<?> parent, View view, int position, long id);

    @Override
    public abstract void onPullDownToRefresh(PullToRefreshBase refreshView);

    @Override
    public abstract void onPullUpToRefresh(PullToRefreshBase refreshView);

    private class ViewHolder {
        @ViewInject(R.id.movie_vp_container)
        private FlashView flashView;

        public ViewHolder(View v) {
            ViewUtils.inject(this, v);
        }
    }

    private void showRotation(List<String> imageUrls) {
        FlashView view = vh.flashView;
        view.setImageUris(imageUrls);
        view.setEffect(EffectConstants.CUBE_EFFECT);//更改图片切换的动画效果
    }
}
